<?php
namespace app\services;

use app\models\User;

class ProfitSettingService extends UsersService {
    public $matrix = [
        'id'     => 'id',
        'name'   => 'name',
        'status' => ['status', '$v==1 && $v="waiting";
                                $v==2 && $v="effective";
                                $v==3 && $v="paused";
                                $v==4 && $v="deleted";
                                $v==5 && $v="overdue";
                                $v==6 && $v="closed";
                                $v==7 && $v="incomplete";
                                $v==8 && $v="incomplete";
                                $v==9 && $v="incomplete";
                                return $v;'],
        'advertiser' => 'advertiser',
        'startDate' => 'start_date',
        'endDate'   => 'end_date',
        'launchTotalBudget' => 'budget',
        'tomorrowMargin' => ['ratio', 'return 100-$v;'],
        'lastModified' => 'last_update',
    ];

    public function getSolutionList() {
        $solutions = $this->getAll('Solution');
        $advertisers = $this->getAll('Advertiser');
        foreach ($solutions as &$solution) {
            $adid = $solution['advertiser_id'];
            $solution['advertiser'] = $advertisers[$adid]['name'];
        }
        return $this->fieldsMap($this->matrix, $solutions);
    }

    public function updateSolutionMargin($solutionId, $margin) {
        return $this->saveOn('Solution', ['id'=>$solutionId], ['ratio' => 100 - $margin]);
    }
}// end of advertiser service declaration

